﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class LaySprinklersAreaForm : System.Windows.Forms.Form
	{
		public LaySprinklersAreaForm(UserSetting userSetting)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.InitCmbDanger();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string limitStr = "1";
			if (this.rdoLimitNumber.Checked)
			{
				this.layAreaParameter.LimitFlag = 1;
				this.layAreaParameter.RowCount = Convert.ToInt32(this.hytbRowCount.Text.Trim());
				this.layAreaParameter.ColCount = Convert.ToInt32(this.hytbColCount.Text.Trim());
				this.layAreaParameter.Ratio = Convert.ToDouble(this.hytbRatio.Text.Trim());
				limitStr = "1";
			}
			else if (this.rdoLimitDistance.Checked)
			{
				if (!this.IsInputNorm())
				{
					return;
				}
				this.layAreaParameter.LimitFlag = 2;
				this.layAreaParameter.RowDistance = Convert.ToDouble(this.hytbRowDistance.Text.Trim());
				this.layAreaParameter.ColDistance = Convert.ToDouble(this.hytbColDistance.Text.Trim());
				this.layAreaParameter.RowDistanceSide = Convert.ToDouble(this.hytbRowDistanceSide.Text.Trim());
				this.layAreaParameter.ColDistanceSide = Convert.ToDouble(this.hytbColDistanceSide.Text.Trim());
				this.layAreaParameter.LessDistance = double.Parse(this.hytbSmallDistance.Text.Trim());
				this.layAreaParameter.Radius = double.Parse(this.hytbRadius.Text.Trim());
				this.layAreaParameter.Danger = this.cmbDanger.Text;
				limitStr = "2";
			}
			this.LaySettingWrite(limitStr);
			base.DialogResult = DialogResult.OK;
		}

		private bool IsInputNorm()
		{
			bool result = true;
			if (this.IsTextBoxEmpty(this.hytbColDistance))
			{
				result = false;
			}
			else if (this.IsTextBoxEmpty(this.hytbRowDistance))
			{
				result = false;
			}
			else if (this.IsTextBoxEmpty(this.hytbRowDistanceSide))
			{
				result = false;
			}
			else if (this.IsTextBoxEmpty(this.hytbColDistanceSide))
			{
				result = false;
			}
			else if (this.IsTextBoxEmpty(this.hytbSmallDistance))
			{
				result = false;
			}
			else if (!this.IsTextBoxNumber(this.hytbColDistance))
			{
				result = false;
			}
			else if (!this.IsTextBoxNumber(this.hytbRowDistance))
			{
				result = false;
			}
			else if (!this.IsTextBoxNumber(this.hytbRowDistanceSide))
			{
				result = false;
			}
			else if (!this.IsTextBoxNumber(this.hytbColDistanceSide))
			{
				result = false;
			}
			else if (!this.IsTextBoxNumber(this.hytbSmallDistance))
			{
				result = false;
			}
			return result;
		}

		private bool IsTextBoxEmpty(System.Windows.Forms.TextBox tb)
		{
			bool result = false;
			if (tb.Text.Trim() == "")
			{
				result = true;
				YJKMessageBox.Information("所有参数均不能为空");
				tb.Focus();
			}
			return result;
		}

		private bool IsTextBoxNumber(System.Windows.Forms.TextBox tb)
		{
			bool flag = true;
			try
			{
				if (Convert.ToDouble(tb.Text.Trim()) <= 0.0)
				{
					YJKMessageBox.Information("所有参数均为正数");
					flag = false;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Information("所有参数均为数字");
				flag = false;
			}
			if (!flag)
			{
				tb.Text = "";
				tb.Focus();
			}
			return flag;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void LayAirTerminalAreaForm_Load(object sender, EventArgs e)
		{
			this.LaySettingInit();
		}

		private void LaySettingInit()
		{
			this.cmbDanger.Text = this.layAreaParameter.Danger;
			this.layAreaParameter = this.userSettingS.GetLaySprinklersArea();
			this.hytbRowCount.Text = this.layAreaParameter.RowCount.ToString();
			this.hytbColCount.Text = this.layAreaParameter.ColCount.ToString();
			this.hytbRatio.Text = this.layAreaParameter.Ratio.ToString();
			this.hytbRowDistance.Text = this.layAreaParameter.RowDistance.ToString();
			this.hytbColDistance.Text = this.layAreaParameter.ColDistance.ToString();
			this.hytbRowDistanceSide.Text = this.layAreaParameter.RowDistanceSide.ToString();
			this.hytbColDistanceSide.Text = this.layAreaParameter.ColDistanceSide.ToString();
			if (this.layAreaParameter.LimitFlag.ToString() == "2")
			{
				this.rdoLimitDistance.Checked = true;
				this.rdoLimitNumber.Checked = false;
				return;
			}
			this.rdoLimitNumber.Checked = true;
			this.rdoLimitDistance.Checked = false;
		}

		private void LaySettingWrite(string limitStr)
		{
			this.userSettingS.WriteLaySprinklersArea(this.layAreaParameter);
		}

		private void rdoLimitNumber_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitNumber.Checked)
			{
				this.rdoLimitDistance.Checked = false;
				this.RdoNumberGroup(true);
				return;
			}
			this.RdoNumberGroup(false);
		}

		private void RdoNumberGroup(bool check)
		{
			this.hytbRowCount.Enabled = check;
			this.hytbColCount.Enabled = check;
			this.hytbRatio.Enabled = check;
		}

		private void RdoDistanceGroup(bool check)
		{
			this.hytbRowDistance.Enabled = check;
			this.hytbColDistance.Enabled = check;
			this.hytbRowDistanceSide.Enabled = check;
			this.hytbColDistanceSide.Enabled = check;
			this.cmbDanger.Enabled = check;
			this.hytbSmallDistance.Enabled = check;
			if (check)
			{
				base.Width = 598;
				return;
			}
			base.Width = 411;
		}

		private void rdoLimitDistance_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitDistance.Checked)
			{
				this.rdoLimitNumber.Checked = false;
				this.RdoDistanceGroup(true);
				return;
			}
			this.RdoDistanceGroup(false);
		}

		private void InitCmbDanger()
		{
			this.cmbDanger.DisplayMember = "Name";
			this.cmbDanger.ValueMember = "MoreDistanceRow";
			this.cmbDanger.Items.Add(new DangerItem("轻危险级", 4400.0, 4400.0, 2400.0, 2200.0, 2200.0, 3100.0));
			this.cmbDanger.Items.Add(new DangerItem("中危险级I", 3600.0, 3600.0, 2400.0, 1800.0, 1800.0, 2500.0));
			this.cmbDanger.Items.Add(new DangerItem("中危险级II", 3400.0, 3400.0, 2400.0, 1700.0, 1700.0, 2400.0));
			this.cmbDanger.Items.Add(new DangerItem("严重危险级I", 3000.0, 3000.0, 2400.0, 1500.0, 1500.0, 2100.0));
			this.cmbDanger.SelectedItem = this.cmbDanger.Items[0];
		}

		private void cmbDanger_SelectedIndexChanged(object sender, EventArgs e)
		{
			DangerItem dangerItem = this.cmbDanger.SelectedItem as DangerItem;
			this.hytbRowDistance.Text = dangerItem.MoreDistanceRow.ToString();
			this.hytbColDistance.Text = dangerItem.MoreDistanceCol.ToString();
			this.hytbSmallDistance.Text = dangerItem.LessDistance.ToString();
			this.hytbRowDistanceSide.Text = dangerItem.SideDistanceRow.ToString();
			this.hytbColDistanceSide.Text = dangerItem.SideDistanceCol.ToString();
			this.hytbRadius.Text = dangerItem.Radius.ToString();
		}

		private void hytbRowDistance_TextChanged(object sender, EventArgs e)
		{
			this.ChangeForeColor(this.hytbRowDistance, 10.0, 10000.0);
		}

		private void hytbColDistance_TextChanged(object sender, EventArgs e)
		{
			this.ChangeForeColor(this.hytbColDistance, 10.0, 10000.0);
		}

		private void hytbSmallDistance_TextChanged(object sender, EventArgs e)
		{
			this.ChangeForeColor(this.hytbSmallDistance, 10.0, 10000.0);
		}

		private void hytbRowDistanceSide_TextChanged(object sender, EventArgs e)
		{
			this.ChangeForeColor(this.hytbRowDistanceSide, 0.0, 10000.0);
		}

		private void hytbColDistanceSide_TextChanged(object sender, EventArgs e)
		{
			this.ChangeForeColor(this.hytbColDistanceSide, 0.0, 10000.0);
		}

		private void ChangeForeColor(System.Windows.Forms.TextBox tb, double min, double max)
		{
			try
			{
				if (Convert.ToDouble(tb.Text) < min || Convert.ToDouble(tb.Text) > max)
				{
					tb.ForeColor = System.Drawing.Color.Red;
				}
				else
				{
					tb.ForeColor = System.Drawing.Color.Black;
				}
			}
			catch (Exception)
			{
			}
		}

		private void hytbRowDistance_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '.' || e.KeyChar == '.')
			{
				string text = ((System.Windows.Forms.TextBox)sender).Text;
				for (int i = 0; i < text.Length; i++)
				{
					if (text[i] == '.')
					{
						e.Handled = true;
						return;
					}
				}
			}
			if (!StringTools.IsNumber(e.KeyChar))
			{
				e.Handled = true;
				return;
			}
			e.Handled = false;
		}

		private HVACUserSetting userSettingS = new HVACUserSetting();

		public LayAreaParameter layAreaParameter = new LayAreaParameter();
	}
}
